var URL_BASE = "http://70.12.110.62:8888/sosList"
$(function() {
	$('#refresh').click( function(){
		var lineNumber = $('#lineNumber').val();
		console.log(lineNumber);
		loadStationList(lineNumber, staionIdx)
	});
	$('#lineNumber').bind('change', function(){
		var lineNumber = $('#lineNumber').val();
		console.log(lineNumber);
		loadStationList(lineNumber, staionIdx)
	});
	$('.updown').bind('change', function(){
		var stationCd = $('#detail').data('data').STATION_CD;
		loadTrainList(stationCd);
	});
	$('#refresh2').click(function(){
		var stationCd = $('#detail').data('data').STATION_CD;
		loadTrainList(stationCd);
	});

});

function loadStationList(lineNumber, startIdx) {
	$.mobile.showPageLoadingMsg();
	$.ajax({
		url : makeUrl(SVC_STATION, "json", startIdx, 5,
				[lineNumber]),
		dataType : 'json',
		success : function(data) {
			if (data.RESULT) {
				showErrorMsg(data.RESULT.CODE, data.RESULT.MESSAGE);
			} else if (data[SVC_STATION]) {
				var $stationList = $('#stationList');
				if (startIdx == 1) {
					$stationList.empty();
				}
				var result = data[SVC_STATION];
				for ( var i = 0; i < result.row.length; i++) {
					$('<li/>').append(
							$('<a class="link" href="#detail"/>')
							.text(result.row[i].STATION_NM)
							.data("data", result.row[i])
							.bind('click', showDetail)
					).appendTo($stationList);

				}
				$stationList.listview('refresh');

				/* if (startIdx + 5 <= result.list_total_count) {
					loadStationList(lineNumber, startIdx + 5);
				 }*/
			}
		},
		error : function(xhr, status, err) {
			showErrorMsg(err);
		},
		complete : function() {
			$.mobile.hidePageLoadingMsg();
		}

	});

}

function showDetail(){
	var data = $(this).data("data");
	console.log(data);
	$('#stationName').text(data.STATION_NM);
	$('#detail').data("data", data);
	loadTrainList(data.STATION_CD);

}

function loadTrainList(stationCd){
	var updown = $('.updown:checked').val();
	$.ajax({
		url : makeUrl(SVC_ARRIVAL, 'json', 1, 5, [stationCd, updown, 1]),
		dataType : 'json', 
		success : function(data){
			//console.log(data);
			if (data.RESULT) {
				showErrorMsg(data.RESULT.CODE, data.RESULT.MESSAGE);
			} else if (data[SVC_ARRIVAL]) {
				var result = data[SVC_ARRIVAL];
				console.log(result);
				var $trainList = $('#trainList').empty();
				for(var i=0; i < result.row.length; i++){
					$trainList.append(
								$('<li/>').append(
									$('<div>도착시간:' + result.row[i].ARRIVETIME +"</div>")		
								).append(
									$('<div>출발시간:' + result.row[i].LEFTTIME +"</div>")		
								).append(
									$('<div>종착역:' + result.row[i].SUBWAYNAME +"</div>")												
								)
							
					);
				}
				$trainList.listview('refresh');
			}

		},
		error : function(xhr, status, err) {
			showErrorMsg(err);
		},
		complete : function() {
			$.mobile.hidePageLoadingMsg();
		}
		
	});
}
function makeUrl(service, type, startIdx, endIdx, param) {
	var url = URL_BASE + "/" + KEY + "/" + type + "/" + service + "/"
			+ startIdx + "/" + endIdx;
	for(var i=0; i < param.length; i++){
		url += "/" + param[i];				
	}
	return url;
}

function showErrorMsg(code, msg) {
	$('#errMsg').text(code + ":" + msg);
	$.mobile.changePage('#errorDialog', {
		transition : "pop",
		role : 'dialog'
	});
}